/* 
  createRouter 用户创建路由实例 
  createWebHistory 用于创建 history 模式的路由
*/
import { createRouter, createWebHistory } from 'vue-router'
// 引入路由组件
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import Detail from '@/views/Detail/index.vue'
import CarList from '@/views/CarList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from '@/views/Pay/PayBack.vue'
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'

const router = createRouter({
  // 配置路由模式为 history
  history: createWebHistory(import.meta.env.BASE_URL),
  // 配置路由路径path 和 路由组件component 对应关系
  routes: [
    {
      path: '/',
      component: Layout,
      children: [   // 配置二级路由
        {
          path: '',  // 将这个二级路由路径置空，这样当匹配到一级路由 / 时，这条 / 的子路由二级路由也会匹配上，从而显示对应的二级路由组件
          component: Home
        },
        {
          path: 'category/:id',   // 二级路由的路由路径不需要添加 /
          component: Category
        },
        {
          path: 'category/sub/:id',  // id是占位符，表示后面会接一条数据，可以通过id获取到这个条数据
          component: SubCategory
        },
        {
          path: 'detail/:id',
          component: Detail
        },
        {
          path: 'cartlist',
          component: CarList
        },
        {
          path: 'checkout',
          component: Checkout
        },
        {
          path: 'pay',
          component: Pay
        },
        {
          path: 'paycallback',
          component: PayBack
        },
        {
          path: 'member',
          component: Member,
          children: [    // 三级路由配置
            {
              path: '', // 将这个三级路由路径置空，这样当匹配到二级路由 member 时，这条member的子路由三级路由也会匹配上，从而显示对应的三级路由组件
              component: UserInfo
            },
            {
              path: 'order',
              component: UserOrder
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      component: Login
    }
  ],
  // 路由滚动行为定制
  scrollBehavior(){
    // 路由切换的时候，滑动到顶部
    return {
      top: 0
    }
  }
})

export default router
